import { createRouter, createWebHistory } from 'vue-router'
import ShowView from '../views/ShowView.vue'
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/activity',
      name: 'activity',
      component: () => import('../views/ActivityView.vue'),
      meta: {
        hideTabBar: true
      }
    },
    {
      path: '/show/search',
      name: 'showsearch',
      component: () => import('../views/Search.vue'),
      meta: {
        hideTabBar: true
      }
      
    },
    {
      path: '/show',
      name: 'ShowView',
      component: ShowView
    },
    {
      path: '/city',
      name: 'city',
      component: () => import('../views/City.vue'),
      meta: {
        hideTabBar: true
      }
    },
    {
      path: '/ticket',
      name: 'ticket',
      component: () => import('../views/TicketView.vue'),
      meta: {
        isUser: true
      },
    },
    {
      path: '/ticket/bind-ticket',
      name: 'bindticket',
      component: () => import('../views/BindTicket.vue')
    },
    {
      path: '/search',
      name: 'search',
      component: () => import('../views/SearchView.vue'),
    },
    {
      path: '/searching',
      name: 'searching',
      component: () => import('../views/Searching.vue')
    },
    {
      path: '/search/user/:myid',
      name: 'User',
      component: () => import('../views/User.vue'),
      children: [
        {
          path: '/search/user/:myid/all',
          name: 'all',
          component: () => import('../views/user/Allview.vue'),
        },
        {
          path: '/search/user/:myid/comments',
          name: 'comments',
          component: () => import('../views/user/CommentsView.vue'),
        },
        {
          path: '/search/user/:myid/wantwatch',
          name: 'wantwatch',
          component: () => import('../views/user/WantwatchView.vue'),
        },
        {
          path: '/search/user/:myid',
          redirect: { name: 'all' }
        },
      ]
    },
    {
      path: '/search/user/following/:id',
      name: 'following',
      component: () => import('../views/user/FollowingView.vue')
    },
    {
      path: '/search/user/follower/:id',
      name: 'follower',
      component: () => import('../views/user/FollowerView.vue')
    },
    {
      path: '/mine',
      name: 'mine',
      component: () => import('../views/UserView.vue'),
    },
    {
      path: '/mine/order',
      name: 'order',
      component: () => import('../views/Userview/Order.vue'),
      meta: {
        isWzhRequired: true
      },
      beforeEnter: (to, from, next) => {
        if (to.meta.isWzhRequired) {
          if (sessionStorage.getItem('token')) {
            next(true)
          } else {
            // next('/login')
            next({
              path: '/login',
              query: { redirect: to.fullPath }
            })
          }
        } else {
          next()
        }
      }
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('../views/Login.vue'),
      meta: {
        hideTabBar: true
      }
    },
    {
      path: '/:pathMatch(.*)*',
      name: 'notfound',
      component: () => import('../views/NotFound.vue'),
      redirect: '/show'
    },

  ]
})

router.beforeEach((to, from, next) => {
  // console.log(to.fullPath)
  if (to.meta.isUser) {
    if (localStorage.getItem('user')) {
      next()
    } else {
      next({
        path: '/login',
        query: { aaa: to.fullPath }
      })
    }
  } else {
    next()
  }
})
export default router
